<?php

declare(strict_types=1);

namespace DreamCat\Array2Class\TestCases;

use DreamCat\Array2Class\Array2ClassConverter;
use DreamCat\Array2Class\Demo\DemoFunc;
use Dreamcat\PropertyAnalysis\Pojo\BuildinType;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;

/**
 * 测试按类型名转换
 * @author vijay
 */
class ConvertByTypeTest extends TestCase
{
    /**
     * 常规测试
     * @return void
     */
    public function testNormal()
    {
        $convert = new Array2ClassConverter();
        $methodRef = new ReflectionMethod(DemoFunc::class, "f");
        $params = $methodRef->getParameters();

        $data = uniqid();
        $param = $params[0];
        $result = $convert->convertByType($data, $param->getType(), $param->getAttributes());
        self::assertEquals($data, $result);

        $data = mt_rand(0, 1000);
        $param = $params[1];
        $result = $convert->convertByType($data, $param->getType(), $param->getAttributes());
        self::assertEquals($data, $result);

        $data = [
            uniqid(),
            mt_rand(0, 1000),
        ];
        $param = $params[2];
        $result = $convert->convertByType($data, $param->getType(), $param->getAttributes());
        self::assertEquals($data, $result);

        $data = ["value" => uniqid()];
        $param = $params[3];
        $result = $convert->convertByType($data, $param->getType(), $param->getAttributes());
        $expected = DemoFunc::builder($data);
        self::assertEquals($expected, $result);

        $data = ["value" => mt_rand(0, 34)];
        $param = $params[4];
        $result = $convert->convertByType($data, $param->getType(), $param->getAttributes());
        $expected = array_values($data);
        self::assertEquals($expected, $result);

        $data = uniqid();
        $result = $convert->convert($data, BuildinType::STRING);
        self::assertEquals($data, $result);
    }
}

# end of file
